package com.zjy.pet.controller;

import com.zjy.basic.domain.dto.AcceptOrderDto;
import com.zjy.basic.util.CurrentLoginInfoUtil;
import com.zjy.org.mapper.EmployeeMapper;
import com.zjy.pet.service.SearchMasterMsgService;
import com.zjy.pet.domain.SearchMasterMsg;
import com.zjy.pet.query.SearchMasterMsgQuery;
import com.zjy.basic.util.AjaxResult;
import com.zjy.basic.util.PageList;
import com.zjy.user.domain.Logininfo;
import com.zjy.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public SearchMasterMsgService searchMasterMsgService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult save(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
                searchMasterMsgService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findById(@PathVariable("id")Long id) {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryPage(query);
    }


    /**
     * 发布寻主信息
     * @param searchMasterMsg
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) throws Exception {
        return searchMasterMsgService.publish(searchMasterMsg,request);
    }

    /**
     * 用户自己看的
     * @param query
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/own")
    public PageList<SearchMasterMsg> own(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) throws Exception {
        //获取到logininfo登录信息
        Logininfo logininfo = CurrentLoginInfoUtil.currentLoginInfo(request);
        //根据logininfo信息获取到user并获取到该User的id
        Long userId = userMapper.findByLoginInfoId(logininfo.getId()).getId();
        //查询个人的
        query.setUserId(userId);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 驳回的 - 管理员看的
     * @param query
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/reject")
    public PageList<SearchMasterMsg> reject(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) throws Exception {
        query.setState(-1);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 寻主池的 - 店铺可以看到所有寻主池的消息
     * @param query
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) throws Exception {
        query.setState(1);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 待处理 - 被分配的店铺可以看到自己店铺待处理的消息
     * @param query
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/handle")
    public PageList<SearchMasterMsg> handle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) throws Exception {
        //获取到logininfo登录信息
        Logininfo logininfo = CurrentLoginInfoUtil.currentLoginInfo(request);
        //根据logininfo信息获取到employee并获取到该employee的id
        Long shopId = employeeMapper.findByLoginInfoId(logininfo.getId()).getShop_id();
        query.setState(1);
        query.setShopId(shopId);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 已完成的 - 所有人都能看
     * @param query
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) throws Exception {
        query.setState(2);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 店铺拒绝接单
     * @param id
     * @return
     */
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id){
        return searchMasterMsgService.reject(id);
    }

    /**
     * 店铺接单
     * @param dto
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptOrderDto dto){
        return searchMasterMsgService.accept(dto);
    }
}
